from typing import Literal
import pydevd_pycharm


from pythongo.base import BaseParams, BaseState, BaseStrategy, Field
from pythongo.classdef import OrderData, TradeData, TickData
from self_strategy.SimpleWindow import SimpleWindow

global kline

class Params(BaseParams):
    """参数映射模型"""
    exchange: str = Field(default="CZCE", title="交易所代码")
    instrument_id: str = Field(default="sa2601", title="合约代码")
    order_price: int | float = Field(default=0, title="报单价格")
    order_volume: int = Field(default=1, title="报单手数")
    order_direction: Literal["buy", "sell"] = Field(default="buy", title="报单方向")
    MACD启动: bool = Field(default=False, title="启动MACD指标")


class State(BaseState):
    """状态映射模型"""
    order_id: int | None = Field(default=None, title="报单编号")


class 策略测试(BaseStrategy):
    """我的第一个策略"""

    def connect_debugger(self,port1=9956):
        try:
            print("尝试连接 PyCharm 调试器...")
            pydevd_pycharm.settrace('localhost', port=port1, stdoutToServer=True, stderrToServer=True)
            print("成功连接到调试器！")
        except Exception as e:
            print(f"连接调试器失败：{e}。继续执行后续代码...")
            # 可以选择 pass 或记录日志，程序会正常继续

    def __init__(self) -> None:
        super().__init__()
        self.params_map = Params()


        #self.output("策略初始化",1123,444)
        a = 10
        b = 30
        c = a + b
        self.state_map = State()
        #pydevd_pycharm.stoptrace()

    def on_order(self, order: OrderData) -> None:
        super().on_order(order)
        self.output("报单信息：", order)

    def on_tick(self, tick: TickData) -> None:
        self.output("", tick)
        self.kline_generator.tick_to_kline(tick)

    def on_start(self) -> None:
        app = SimpleWindow()
        app.start()
        self.output("策略启动111")
        super().on_start()
        #list = self.params_map.exchange.split(";")
        #macdStart = self.params_map.MACD启动
        #if macdStart:
            #self.output("MACD指标启动")
        #for x in list:
           # self.get_instrument_data(x)
            #self.output("交易所数据:" + x)
        #绘制一个pyqt 窗口

        # self.state_map.order_id = self.send_order(
        #     exchange=self.params_map.exchange,
        #     instrument_id=self.params_map.instrument_id,
        #     volume=self.params_map.order_volume,
        #     price=self.params_map.order_price,
        #     order_direction=self.params_map.order_direction
        # )

        self.update_status_bar()

    def on_stop(self) -> None:
        super().on_stop()
        self.output("策略暂停")
        #pydevd_pycharm.stoptrace()


